/*
   Copyright 2009 Bégaudeau Stéphane

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package faneclipse.wizards;

import faneclipse.builder.FanNature;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;

import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

/**
 * @author BEGAUDEAU stephane
 */
public class FanProjectWizard extends Wizard implements INewWizard {
	/**
	 * The page of the wizard.
	 */
	private FanFirstPageProjectWizard firstPage;
	
	/**
	 * the constructor.
	 */
	public FanProjectWizard() {
		this.setWindowTitle("New Fan Project");
	}
	
	/**
	 * @see org.eclipse.jface.wizard.Wizard#addPages()
	 */
	public final void addPages() {
    	firstPage = new FanFirstPageProjectWizard("New Fan project");
    	this.addPage(firstPage);
    }

	/**
	 * @return A boolean showing if we can finish the action of the wizard.
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	@Override
	public final boolean performFinish() {
		boolean projectOk = false;
		if(!"".equals(firstPage.getProjectName()))
		{
			IProgressMonitor progressMonitor = new NullProgressMonitor();
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			IProject project = root.getProject(firstPage.getProjectName());
			try
			{
				if (!project.exists())
				{
					project.create(progressMonitor);
				}
				project.open(progressMonitor);
				IProjectDescription description = project.getDescription();
				String[] natures = description.getNatureIds();
				String[] newNatures = new String[natures.length + 1];
				System.arraycopy(natures, 0, newNatures, 0, natures.length);
				newNatures[natures.length] = FanNature.NATURE_ID;
				description.setNatureIds(newNatures);
				project.setDescription(description, progressMonitor);
				projectOk = true;
			}
			catch (CoreException e)
			{
				e.printStackTrace();
			}
		}
		return projectOk;
	}

	/**
	 * @param workbench the workbench.
	 * @param selection The selection.
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
	 */
	public final void init(final IWorkbench workbench, final IStructuredSelection selection) {
		
	}
}
